define(function(require, exports){
	var $ = require("jquery");
	var pubjs = require('pubjs');
	var view = require('@base/view');
	var util = require('util');
	var dspUtil = require('dspUtil');

	var Base = view.container.extend({
		init:function(config){
			config = pubjs.conf(config, {
				'class': 'P-PtcRadio',
				'view_model': {
					'data': []
					,'vmRemoveItem': this.vmRemoveItem
				}
			});

			this.$guid = 0;
			this.Super('init', arguments);
		}
		,afterBuild:function(){
			this.append(`
				<div ms-repeat-item="data" class="container">
                    <div class="">
                        <label class="">名称:</label>
                        <label class="mr15">
                            <input type="text" ms-duplex="item.name">
                        </label>

                        <label class="">值:</label>
                        <label class="mr15">
                            <input type="text" ms-duplex="item.value">
                        </label>

                        <input type="button" class="operate btn btn-danger" value="Del" ms-click="vmRemoveItem($index)"/>
                        <!--div class="ptcDropdown" >
                            <label class="">类型:</label>
							<span ms-attr-id="dropdown_{{item.gid}}"/>
						</div-->
                    </div>
				</div>
				<div class="mb10">
					<span pub-name="add" pub-mod="@base/common/input.button"/>
				</div>
			`);

			this.buildFromTemplate({
				add: {
					'value': 'Add'
					,'class': 'btn btn-success'
					,'width': 50
				}
			});
		}
		,vmRemoveItem(index)
		{
			var data = this.vm.$.data;
			if(data && data.length)
			{
				data.splice(index, 1);
			}
		}
		,_getGuid: function()
		{
			return ++this.$guid;
		}
		,onInputClick: function(ev) {
			switch (ev.name) {
				case 'add':
					var vm = this.vm.$;
					vm.data.push({name: '', value: '', gid: this._getGuid()});
					//this.createAsyncModule();
					break;
				default :
					return false;
			}
		}
		,createAsyncModule: function() {
			var self = this;
			var data = self.vm.$.data;
			var ch = self.$;
			if(data && data.length) {
				for(var i = 0; i < data.length; i++) {
					name = 'PTDropdown_'+ data[i].gid;
					var old = data[i].type;
					if(ch && ch[name]) {
						old = ch[name].getData();
						ch[name].destroy();
					}

					var dropdownParam = {
						'target': self.$el.find('#dropdown_' + data[i].gid)
					};

					self.createAsync(name, '@base/common/dropdown.base', $.extend({}, {
						'options': [
							{_id: "number", Name: LANG('Number')}
							,{_id: "string", Name: LANG('String')}
							,{_id: "boolean", Name: LANG('Boolean')}
						]
						,'def': LANG("请选择")
						,'old_value': old
						,'width': 120
					}, dropdownParam), function(mod) {
						var value = mod.getConfig('old_value') || "string";
						mod.setData(value);
					});
				}
			}
		}
		,validate: function(data){
			if(!data || !data.length)
			{
				pubjs.alert('选项不能为空');
				return false;
			}

			var name = {};
			var value = {};
			for(var i = 0; i < data.length; i++)
			{
				if(!data[i].name)
				{
					pubjs.alert('属性名不能为空');
					return false;
				}
				if(!data[i].value)
				{
					pubjs.alert('请检查值: ', data[i].name);
					return false;
				}
				if(name[data[i].name])
				{
					pubjs.alert('属性重复: ', data[i].name);
					return false;
				}

				name[data[i].name] = true;
				if(value[data[i].value])
				{
					pubjs.alert('键重复: ', data[i].value);
					return false;
				}

				value[data[i].value] = true;
			}

			return true;
		}
		,setData: function(data) {
			if(data && data.length)
			{
				var self = this;
				var vm = self.vm.$;
				vm.data = [];
				util.each(data, function(item) {
					var t = {name: item.name, value: item.value, gid: self._getGuid()};
					//t.type = self._getType(item.value);
					vm.data.push(t);
				});
			}
			//this.createAsyncModule();
		}
		,_getType: function(value) {
			return typeof value;
		}
		,_getTypeValue: function(v, type)
		{
			switch (type)
			{
				case 'string':
					return String(v);
				case 'number':
					return Number(v);
				case 'boolean':
					return !!v;
			}
		}
		,getData: function(){
			var data = this.vm.$.data;
			//var ch = this.$;
			var ret = [];
			if(data && data.length)
			{
				for(var i = 0; i < data.length; i++)
				{
					var item = data[i];
					// 暂时先支持字符串
					ret.push({
						name: item.name
						,value: String(item.value)
					});

					//var ch_name = 'PTDropdown_' + item.gid;
					//if(ch[ch_name])
					//{
					//	var type = ch[ch_name].getData();
					//	ret.push({
					//		name: item.name
					//		,value: this._getTypeValue(item.value, type)
					//		//,type: type
					//	})
					//}
				}
			}

			return ret;
		}
		,reset: function(){
			this.vm.reset();
			return this;
		}
	});

	exports.base = Base;

});